package it.appcatalogobase;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CategoriaDataSource {
	 // Database fields
	  private SQLiteDatabase database;
	  private CatalogoOpenHelper dbHelper;
	  private String[] allColumns = { CatalogoOpenHelper.COLUMN_ID_CAT,
			  CatalogoOpenHelper.COLUMN_NAME_CAT, CatalogoOpenHelper.COLUMN_DES_CAT };

	  public CategoriaDataSource(Context context) {
	    dbHelper = new CatalogoOpenHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Categoria createCategoria(String categoria, String descrizione) {
	    ContentValues values = new ContentValues();
	    values.put(CatalogoOpenHelper.COLUMN_NAME_CAT, categoria);
	    values.put(CatalogoOpenHelper.COLUMN_DES_CAT, descrizione);
	    long insertId = database.insert(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, null,
	        values);
	    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
	        allColumns, CatalogoOpenHelper.COLUMN_ID_CAT + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Categoria newCategoria = cursorToCategoria(cursor);
	    cursor.close();
	    return newCategoria;
	  }

	  public void deleteCategoria(Categoria cat) {
	    long id = cat.getId();
	    System.out.println("Comment deleted with id: " + id);
	    database.delete(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, CatalogoOpenHelper.COLUMN_ID_CAT
	        + " = " + id, null);
	  }
	  
	  public void deleteCategorie() {
		    database.delete(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE, null, null);
		  }

	  public List<Categoria> getAllCategorie() {
	    List<Categoria> categorie = new ArrayList<Categoria>();

	    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Categoria categoria = cursorToCategoria(cursor);
	      categorie.add(categoria);
	      cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return categorie;
	  }
	  
	  public Categoria getCategoria(Long id) {
//		    List<Categoria> categorie = new ArrayList<Categoria>();

		    Cursor cursor = database.query(CatalogoOpenHelper.DICTIONARY_TABLE_NAME_CATEGORIE,
		        allColumns, "id="+id, null, null, null, null);

		    cursor.moveToFirst();
//		    while (!cursor.isAfterLast()) {
		      Categoria categoria = cursorToCategoria(cursor);
//		      categorie.add(categoria);
		      cursor.moveToNext();
//		    }
		    // make sure to close the cursor
		    cursor.close();
		    return categoria;
		  }

	  private Categoria cursorToCategoria(Cursor cursor) {
	    Categoria categoria = new Categoria();
	    categoria.setId(cursor.getLong(0));
	    categoria.setNome(cursor.getString(1));
	    categoria.setDescrizione(cursor.getString(2));
	    return categoria;
	  }
}
